var _ = require('lodash');
var rp = require('request-promise');
var settings = require('../../config');
var { lookup } = require('lookup-dns-cache');
var { json: jsonData } = require('../helpers/response');
async function request(opts) {
    const method = opts.method || 'GET';
    const json = (typeof opts.json === 'undefined') ? true : opts.json;
    opts.headers = opts.headers || {};
    if (!_.has(opts.headers, 'Content-Type')) {
        opts.headers['Content-Type'] = 'application/json';
    }
    const body = opts.body || {};
    let uri = settings.BASE_URL;
    const requestOptions = {
        timeout: 100000,
        uri: uri + opts.path,
        headers: opts.headers,
        resolveWithFullResponse: true,
        qs: method === 'POST' ? {} : opts.body || {},
        body,
        method,
        json,
        lookup: lookup
    }
    let response = null;

    try {
        response = await rp(requestOptions);
    } catch (e) {
        response = jsonData(e);
    }
    console.log(response)
    if (response.statusCode === 204) {
        // http 204 不返回给前端
        return { deleted: true };
    }
    if (response.statusCode === 200) {
        return response.body;
    }
    return response;
}
module.exports = request